package com.zhongli.devplatform.service.impl.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.dto.common.ClazzDTO;
import com.zhongli.devplatform.dto.student.StudentDTO;
import com.zhongli.devplatform.dto.teacher.TransClazzDTO;
import com.zhongli.devplatform.enums.UserType;
import com.zhongli.devplatform.mapper.clazz.ReadClazzGradeMapper;
import com.zhongli.devplatform.mapper.clazz.ReadClazzMapper;
import com.zhongli.devplatform.mapper.clazz.ReadClazzNoticeMapper;
import com.zhongli.devplatform.mapper.clazz.ReadClazzOperationMapper;
import com.zhongli.devplatform.mapper.school.ReadSchoolMapper;
import com.zhongli.devplatform.mapper.user.ReadTeachClazzMapper;
import com.zhongli.devplatform.mapper.user.ReadUserMapper;
import com.zhongli.devplatform.po.common.ReadClazzPO;
import com.zhongli.devplatform.po.common.ReadSchoolPO;
import com.zhongli.devplatform.po.common.ReadTeachClazzPO;
import com.zhongli.devplatform.po.user.ReadUserPO;
import com.zhongli.devplatform.query.sys.ClazzQuery;
import com.zhongli.devplatform.service.imp.common.ReadClazzService;
import com.zhongli.devplatform.bean.Res;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.vo.common.ClazzVO;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 班级表(ReadClazz)表服务实现类
 *
 * @author aiyuan
 * @since 2020-07-30 16:52:18
 */
@Service
public class ReadClazzServiceImpl extends ServiceImpl<ReadClazzMapper, ReadClazzPO> implements ReadClazzService {

    @Resource
    private ReadClazzMapper clazzMapper;

    @Autowired
    private ReadTeachClazzMapper teachClazzMapper;

    @Autowired
    private ReadUserMapper userMapper;

    @Autowired
    private ReadSchoolMapper schoolMapper;

    @Override
    public List<ReadClazzPO> getAllClazzByGrade(Integer currentUserId, Integer id,Integer schoolId) {
        return clazzMapper.getAllClazzByGrade(currentUserId,id,schoolId);
    }

    @Transactional
    @Override
    public Res removeStudent(Integer clazzId,Integer studentId) {
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        ReadTeachClazzPO teachClazzPO = teachClazzMapper.selectOne(new QueryWrapper<ReadTeachClazzPO>()
                .eq("teacher_id", currentUserId)
                .eq("clazz_id",clazzId).eq("is_admin_teacher",true));
        if (Objects.isNull(teachClazzPO)){
            return Res.not_ok("您不是班主任，无法移除该学生");
        }
        //移出班级所有学生
        ReadUserPO userPO = userMapper.selectOne(new QueryWrapper<ReadUserPO>().eq("id",studentId).eq("clazz_id",clazzId));
        userPO.setClazzId(null);
        userMapper.updateById(userPO);
        return Res.ok("移除成功");
    }

    @Override
    public Res<List<ClazzVO>> getCurrentClazzList(Integer gradeId, Integer gradeType) {
        Integer userId = SecurityHelper.getCurrentUserId();
        return Res.ok(clazzMapper.getCurrentClazzList(userId,gradeId,gradeType));
    }

    @Override
    public Res joinClazz(ClazzDTO clazzDTO) {
        Integer userId = SecurityHelper.getCurrentUserId();
        ReadTeachClazzPO teachClazzPO = teachClazzMapper.selectOne(new QueryWrapper<ReadTeachClazzPO>()
                .eq("teacher_id", userId)
                .eq("clazz_id", clazzDTO.getId())
                .eq("is_admin_teacher",true));
        if (Objects.isNull(teachClazzPO)){
            return Res.not_ok("您不是班主任");
        }
        ReadUserPO userPO = userMapper.selectOne(new QueryWrapper<ReadUserPO>().eq("account_num", clazzDTO.getAccountNum()).eq("category", UserType.Student.getCode()));
        if (Objects.isNull(userPO)){
            return Res.not_ok("用户不存在或者不是学生");
        }
        userPO.setClazzId(clazzDTO.getId());
        userMapper.updateById(userPO);
        return new Res<>(0,"加入成功",null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Res transClass(TransClazzDTO transClazzDTO) {
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        ReadTeachClazzPO oldAdminTeacher = teachClazzMapper.selectOne(new QueryWrapper<ReadTeachClazzPO>()
                .eq("teacher_id", currentUserId)
                .eq("clazz_id",transClazzDTO.getClazzId()).eq("is_admin_teacher",true));
        if (Objects.isNull(oldAdminTeacher)){
            return Res.not_ok("您不是班主任，无法转让班级");
        }
        //将当前老师改为普通老师
        oldAdminTeacher.setIsAdminTeacher(false);
        teachClazzMapper.update(oldAdminTeacher,new QueryWrapper<ReadTeachClazzPO>()
                .eq("teacher_id", currentUserId)
                .eq("clazz_id",transClazzDTO.getClazzId()));
        //开始转让
        ReadTeachClazzPO teachClazzPO = teachClazzMapper.selectOne(new QueryWrapper<ReadTeachClazzPO>()
                .eq("clazz_id", transClazzDTO.getClazzId())
                .eq("teacher_id",transClazzDTO.getTeacherId()));
        //如果该老师没有任课该班级就添加一个
        if (Objects.isNull(teachClazzPO)){
            ReadTeachClazzPO readTeachClazzPO = new ReadTeachClazzPO();
            readTeachClazzPO.setIsAdminTeacher(true);
            readTeachClazzPO.setTeacherId(transClazzDTO.getTeacherId());
            readTeachClazzPO.setClazzId(transClazzDTO.getClazzId());
            teachClazzMapper.insert(readTeachClazzPO);
        }else {
            teachClazzPO.setIsAdminTeacher(true);
            teachClazzMapper.update(teachClazzPO,new UpdateWrapper<ReadTeachClazzPO>()
                    .eq("clazz_id", transClazzDTO.getClazzId())
                    .eq("teacher_id",transClazzDTO.getTeacherId()));
        }
        return new Res<>(0,"转让成功",null);
    }


}
